import UIKit
import GoogleSignIn

class GoogleSignInViewController: UIViewController, GIDSignInDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 设置登录按钮
        let signInButton = GIDSignInButton()
        signInButton.frame = CGRect(x: 100, y: 200, width: 200, height: 44)
        view.addSubview(signInButton)

        // 设置代理
        GIDSignIn.sharedInstance().delegate = self
    }

    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if let error = error {
            print("登录出错: \(error.localizedDescription)")
            return
        }

        // 登录成功，获取用户信息
        let idToken = user.authentication.idToken // 用于后端验证的 ID 令牌
        let accessToken = user.authentication.accessToken // 访问令牌
        let name = user.profile.name
        let email = user.profile.email

        print("登录成功，用户姓名: \(name ?? "")，邮箱: \(email ?? "")")
    }
}
    